<template>
  <div class="digital-container">
    <div v-if="views.length > 1" class="header-row">
      <h2 class="section-title">{{ views[6].name }}</h2>
      <div class="tag-list">
        <a
          v-for="(tab, index) in views[6].palteTabList"
          :key="tab.id"
          :href="tab.jumpUrl"
          class="tag-item"
        >
          {{ tab.name }}
          <span
            v-if="index < views[6].palteTabList.length - 1"
            class="separator"
            >/</span
          >
        </a>
      </div>
    </div>
    <Myswiper6></Myswiper6>
    <div class="two-image-section" v-if="platePicList.length > 0">
      <div
        class="image-wrapper"
        v-for="(image, index) in platePicList"
        :key="index"
      >
        <img
          :src="image.imgUrl"
          :alt="'Promotion image ' + (index + 1)"
          class="promo-image"
        />
      </div>
    </div>

    <div class="product-section" v-if="goodsList.length > 0">
      <div class="product-grid">
        <div
          v-for="item in goodsList.slice(0, 4)"
          :key="item.id"
          class="product-card"
          @click.stop="goToGoodsDetail(item.goodsId)"
        >
          <div
            class="product-image-container"
            @mouseover="hoverProduct = item.id"
            @mouseleave="hoverProduct = null"
          >
            <img
              :src="item.imgUrl"
              :alt="item.goodsName"
              class="product-image"
              :class="{ 'product-image-hover': hoverProduct === item.id }"
            />
            <span v-if="item.isNew === 1" class="new-badge">新品</span>
          </div>
          <div class="product-info">
            <h3 class="product-name">{{ item.goodsName }}</h3>
            <p class="product-price">¥{{ item.price.toLocaleString() }}</p>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { getPCPlateContent } from "@/api";
import Myswiper6 from "../vue-awesome-swiper/swiper6.vue";

export default {
  name: "Season",
  components: {
    Myswiper6,
  },
  data() {
    return {
      goodsList: [], //商品信息
      views: [],
      palteTabList: [], //头部列表
      platePicList: [], // 两张图片
      hoverProduct: null,
    };
  },
  methods: {
    goToGoodsDetail(goodsId) {
      this.$router
        .push({
          name: "GoodsDetail",
          params: {
            id: goodsId,
          },
        })
        .catch((err) => {
          if (!err.message.includes("Avoided redundant navigation")) {
            console.error("路由跳转错误:", err);
          }
        });
    },
  },
  async created() {
    try {
      const response = await getPCPlateContent();
      this.views = response.result.views;
      this.goodsList = this.views[6].goodsList;
      this.palteTabList = this.views[6].palteTabList;
      this.platePicList = this.views[6].platePicList || [];
    } catch (error) {
      console.error("加载商品数据失败:", error);
    }
  },
};
</script>

<style scoped>
.digital-container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px;
}

.header-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
  padding: 0 10px;
}

.section-title {
  font-size: 22px;
  font-weight: bold;
  color: #333;
  margin: 0;
}

.tag-list {
  display: flex;
  align-items: center;
}

.tag-item {
  color: #666;
  text-decoration: none;
  font-size: 14px;
  transition: color 0.3s;
  padding: 0 5px;
}

.tag-item:hover {
  color: #1890ff;
}

.separator {
  color: #ddd;
  margin-left: 5px;
}

.two-image-section {
  display: flex;
  gap: 20px;
  margin: 30px 0;
}

.image-wrapper {
  flex: 1;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  transition: transform 0.3s;
}

.image-wrapper:hover {
  transform: translateY(-5px);
}

.promo-image {
  width: 100%;
  height: auto;
  display: block;
}

.product-section {
  margin-top: 40px;
}

.product-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 20px;
}

.product-card {
  border: 1px solid #eee;
  border-radius: 8px;
  overflow: hidden;
  background: white;
  transition: box-shadow 0.3s;
}

.product-card:hover {
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.product-image-container {
  position: relative;
  width: 100%;
  height: 0;
  padding-bottom: 100%;
  overflow: hidden;
}

.product-image {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  object-fit: contain;
  transition: transform 0.3s;
}

.product-image-hover {
  transform: scale(1.05);
}

.product-info {
  padding: 15px;
  text-align: left;
}

.product-name {
  font-size: 14px;
  margin: 0 0 8px 0;
  line-height: 1.4;
  height: 2.8em;
  overflow: hidden;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  text-overflow: ellipsis;
}

.product-price {
  color: #f56c6c;
  font-size: 16px;
  font-weight: bold;
  margin: 0 0 8px 0;
}

.new-badge {
  position: absolute;
  top: 10px;
  left: 10px;
  background-color: #ff4d4f;
  color: white;
  padding: 3px 8px;
  font-size: 12px;
  font-weight: bold;
  border-radius: 4px;
  z-index: 1;
  animation: pulse 2s infinite;
}

@keyframes pulse {
  0% {
    transform: scale(1);
  }
  50% {
    transform: scale(1.05);
  }
  100% {
    transform: scale(1);
  }
}
</style>